package com.panopset.flywheel.gui;

import static com.panopset.Util.*;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.io.StringWriter;
import java.util.Vector;

import javax.swing.JTabbedPane;

import com.panopset.flywheel.Command;
import com.panopset.flywheel.Flywheel;
import com.panopset.flywheel.MatchableCommand;
import com.panopset.gui.Osgw;
import com.panopset.gui.TPanel;

/**
 *
 * @author Karl Dinwiddie
 */
public class FlywheelInfrastruture {

    private final Flywheel flywheel;
    public final int H = 1000;
    public final int W = 1000;
    private TPanel panel;

    /**
     * Wrap a Flywheel instance with debugging infrastructure.
     * @param flywheel
     */
    public FlywheelInfrastruture(Flywheel flywheel) {
        this.flywheel = flywheel;
    }

    public TPanel getPanel() {
        if (panel == null) {
            panel = new TPanel.Builder().layout(new GridLayout(1, 1)).construct();
            JTabbedPane views = new JTabbedPane();
            views.add(getTopCommandCanvas().getPanel());
            views.add(getSubCommandCanvas().getPanel());
            panel.add(views);
        }
        return panel;
    }
    public final Color BG = new Color(0xee, 0xee, 0xff);

    String getCommandVarialTitle(Command command) {
        StringWriter sw = new StringWriter();
        if (command == null) {
            sw.append("null command");
        } else {
            String s = command.getClass().getSimpleName();
            if (s.indexOf(COMMAND) == 0) {
                s = s.substring(COMMAND.length());
            }
            sw.append(s);
        }
        return sw.toString();
    }
    public static final String COMMAND = "Command";
    private TopCommandCanvas topCommandCanvas;

    private TopCommandCanvas getTopCommandCanvas() {
        if (topCommandCanvas == null) {
            topCommandCanvas = new TopCommandCanvas();
            topCommandCanvas.getPanel().setName("Top Commands");
        }
        return topCommandCanvas;
    }
    private SubCommandCanvas subCommandCanvas;

    private SubCommandCanvas getSubCommandCanvas() {
        if (subCommandCanvas == null) {
            subCommandCanvas = new SubCommandCanvas();
            subCommandCanvas.getPanel().setName("Sub Commands");
        }
        return subCommandCanvas;
    }

    public void quit() {
        Osgw.close();
        if (flywheel != null) {
            flywheel.stop(x("Exiting application."));
        }
    }

    class SubCommandCanvas extends CommandCanvas {

        @Override
        public Vector<Command> getDisplayedCommands() {
            if (flywheel.isTargetDirectoryValid()) {
                if (flywheel.resolvingCommand == null) {
                    return null;
                }
                if (flywheel.resolvingCommand instanceof MatchableCommand) {
                    return ((MatchableCommand) flywheel.resolvingCommand).commands;
                }
            }
            return null;
        }
    }

    class TopCommandCanvas extends CommandCanvas {

        @Override
        public Vector<Command> getDisplayedCommands() {
            if (flywheel.isTargetDirectoryValid()) {
                return flywheel.getTemplate().getTopCommands();
            } else {
                return new Vector<Command>();
            }
        }
    }

    abstract class CommandCanvas extends Osgw {

        public CommandCanvas() {
            super(W, H);
        }

        public abstract Vector<Command> getDisplayedCommands();

        @Override
        public void paintCycle(Graphics2D g) {
            g.setColor(BG);
            g.fillRect(0, 0, H, W);

            Vector<Command> cmds = getDisplayedCommands();

            if (cmds == null || !flywheel.isTargetDirectoryValid()) {
                return;
            }

            int x = 6;
            int y = 40;
            int w = 800;
            int h = 15;

            for (Command command : cmds) {
                g.setFont(MONOSPACE_PLAIN_10);
                g.setColor(Color.BLUE);
                g.drawRect(x, y - h + 2, w, h);
                g.setColor(command == flywheel.resolvingCommand ? Color.YELLOW : Color.DARK_GRAY);
                g.fillRect(x, y - h + 2, w, h);
                g.setColor(Color.LIGHT_GRAY);
                g.drawString(getCommandVarialTitle(command), x + 2, y);
                g.setColor(Color.PINK);
                g.drawString(command.toString(), x + 50, y);
                y += 18;
            }
        }
    }
}
